package com.gewu.pm.ui.adapter.delivery;

import android.content.Context;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.gewu.pm.R;
import com.gewu.pm.app.AppAdapter;
import com.gewu.pm.bean.DeliveryGoodsBean;
import com.gewu.pm.http.glide.GlideApp;

/**
 * 配送商品列表适配器
 */
public class DeliveryGoodsAdapter extends AppAdapter<DeliveryGoodsBean> {

    public DeliveryGoodsAdapter(Context context) {
        super(context);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ItemViewHolder();
    }

    private final class ItemViewHolder extends AppAdapter<?>.ViewHolder {

        private final ImageView mGoodsImageView;
        private final TextView mGoodsNameView;
        private final TextView mGoodsCodeView;
        private final TextView mSpecView;
        private final TextView mUnitPriceView;
        private final TextView mDeliveryQuantityView;
        private final TextView mDeliveryAmountView;

        private ItemViewHolder() {
            super(R.layout.delivery_goods_item);
            mGoodsImageView = findViewById(R.id.iv_goods_image);
            mGoodsNameView = findViewById(R.id.tv_goods_name);
            mGoodsCodeView = findViewById(R.id.tv_goods_code);
            mSpecView = findViewById(R.id.tv_spec);
            mUnitPriceView = findViewById(R.id.tv_unit_price);
            mDeliveryQuantityView = findViewById(R.id.tv_delivery_quantity);
            mDeliveryAmountView = findViewById(R.id.tv_delivery_amount_value);
        }

        @Override
        public void onBindView(int position) {
            DeliveryGoodsBean item = getItem(position);
            
            // 设置商品名称
            mGoodsNameView.setText(item.getGoodsName());
            
            // 设置编码
            mGoodsCodeView.setText("编码:" + item.getGoodsCode());
            
            // 设置规格
            mSpecView.setText("规格:" + item.getSpec());
            
            // 设置单价
            mUnitPriceView.setText("单价(盒):¥" + (int)item.getUnitPrice());
            
            // 设置发货数量
            mDeliveryQuantityView.setText("发货数量:" + item.getDeliveryQuantity());
            
            // 设置发货金额
            mDeliveryAmountView.setText("¥" + (int)item.getDeliveryAmount());
            
            // 加载商品图片（使用测试图片）
            int imageRes = R.drawable.test_1;
            if (position % 3 == 1) {
                imageRes = R.drawable.test_2;
            } else if (position % 3 == 2) {
                imageRes = R.drawable.test_3;
            }
            
            GlideApp.with(getContext())
                    .load(imageRes)
                    .into(mGoodsImageView);
        }
    }
}

